from pyspades.server import block_action
from pyspades.collision import distance_3d_vector
from commands import add, admin
from map import Map
from pyspades.constants import *
import commands

@admin
def blockshell(connection):
    if connection.protocol.blockshell:
        connection.protocol.send_chat('blockshell rounds are now off.')
        connection.protocol.blockshell = False
    else:
        if connection.protocol.blockshell is None:
            connection.protocol.blockshell = set()
        connection.protocol.send_chat('blockshell rounds are now on.')
        connection.protocol.blockshell = True
add(blockshell)

def apply_script(protocol, connection, config):
    class blockshellConnection(connection):
        def on_block_removed(self, x, y, z):
            if self.protocol.blockshell == True and self.tool == 3:
                for nade_x in xrange(x - 1, x + 2):
                    for nade_y in xrange(y - 1, y + 2):
                        for nade_z in xrange(z - 1, z + 2):
                            if nade_x > 0 and nade_x < 512 and nade_y > 0 and nade_y < 512 and nade_z > 0 and nade_z < 63:
                                block_action.x = nade_x
                                block_action.y = nade_y
                                block_action.z = nade_z
                                block_action.value = BUILD_BLOCK
                                self.protocol.send_contained(block_action, save = True)
                                self.protocol.map.set_point(nade_x, nade_y, nade_z, (255, 0, 0, 255), user = False)
            return connection.on_block_removed(self, x, y, z)
    class blockshellProtocol(protocol):
        blockshell = None
    return blockshellProtocol, blockshellConnection